if(window) {
    /**
     * 绘制指定边数或角数的多边形或多角星
     * @param {Number} cx 中心点横坐标
     * @param {Number} cy 中心点纵坐标
     * @param {Number} r 中心点到顶点的距离
     * @param {Number} ac 边数或角数
     * @param {String} type 绘制类型，可用值：polygon（多边形）、polyhorn（多角星）
     * @return {Array} 顶点数组
     */
    window.PolygonAndPolyhorns = (function() {

        function PolygonAndPolyhorns(cx, cy, r, ac, type) {
            var perAngleRadian, perAngle, result = [];
            cx = cx || 0;
            cy = cy || 0;
            r = r <= 0 ? 30 : r;
            ac = ac < 3 ? 5 : ac;
            switch (type) {
                case 'polygon': break;
                case 'polyhorn': break;
                default: throw new Error('参数错误type只能为"polygon"或"polyhorn"');
            }
            if(type === 'polyhorn') {
                switch (ac) {
                    case 3: result = triangleStar(cx, cy, r); break;
                    case 4: result = foreStar(cx, cy, r); break;
                    case 6: result = sixStar(cx, cy, r); break;
                    default: 
                        result = handleCurrency(cx, cy, r, ac, type);
                        break;
                }
            } else {
                result = handleCurrency(cx, cy, r, ac, type);
            }
            return result;
        }

        function handleCurrency(cx, cy, r, ac, type) {
            var curRad = getDegree(ac, type);
            var result = [];
            if(curRad > 0) {
                for(var i = 0; i < ac; i += 1) {
                    result.push({
                        x: cx + r * Math.sin(curRad * i),
                        y: cy + r * Math.cos(curRad * i)
                    })
                }
                return result;
            }
        }

        //三角星
        function triangleStar(cx, cy, r) {
            var result = [];
            var outter = handleCurrency(cx, cy, r, 3, 'polygon');
            var scale = .7;
            var inner = [{
                x: cx + r * scale * Math.cos(degToRad(30)), y: cy + r * scale * Math.sin(degToRad(30))
            },{
                x: cx + r * scale * Math.cos(degToRad(270)), y: cy + r * scale * Math.sin(degToRad(270))
            },{
                x: cx + r * scale * Math.cos(degToRad(150)), y: cy + r * scale * Math.sin(degToRad(150))
            }];
            for(var i = 0; i < outter.length; i += 1) {
                result.push(outter[i]);
                result.push(inner[i]);
            }
            return result;
        }

        //四角星
        function foreStar(cx, cy, r) {
            var result = [];
            var outter = handleCurrency(cx, cy, r, 4, 'polygon');
            var scale = .7;
            var inner = [{
                x: cx + r * Math.cos(degToRad(45)) * scale, y: cy + r * Math.sin(degToRad(45)) * scale
            }, {
                x: cx + r * Math.cos(degToRad(315)) * scale, y: cy + r * Math.sin(degToRad(315)) * scale
            }, {
                x: cx + r * Math.cos(degToRad(225)) * scale, y: cy + r * Math.sin(degToRad(225)) * scale
            }, {
                x: cx + r * Math.cos(degToRad(135)) * scale, y: cy + r * Math.sin(degToRad(135)) * scale
            }];
            for(var i = 0; i < outter.length; i += 1) {
                result.push(outter[i]);
                result.push(inner[i]);
            }
            return result;
        }

        //六角星
        function sixStar(cx, cy, r) {
            var result = [];
            var outter = handleCurrency(cx, cy, r, 6, 'polygon');
            var r1 = r * Math.cos(degToRad(30));
            var scale = .7;
            var inner = [{
                x: cx + r1 * Math.cos(degToRad(60)) * scale, y: cy + r1 * Math.sin(degToRad(60)) * scale
            }, {
                x: cx + r1 * Math.cos(degToRad(0)) * scale, y: cy + r1 * Math.sin(degToRad(0)) * scale
            }, {
                x: cx + r1 * Math.cos(degToRad(300)) * scale, y: cy + r1 * Math.sin(degToRad(300)) * scale
            }, {
                x: cx + r1 * Math.cos(degToRad(240)) * scale, y: cy + r1 * Math.sin(degToRad(240)) * scale
            }, {
                x: cx + r1 * Math.cos(degToRad(180)) * scale, y: cy + r1 * Math.sin(degToRad(180)) * scale
            }, {
                x: cx + r1 * Math.cos(degToRad(120)) * scale, y: cy + r1 * Math.sin(degToRad(120)) * scale
            }];
            for(var i = 0; i < outter.length; i += 1) {
                result.push(outter[i]);
                result.push(inner[i]);
            }
            return result;
        }

        //获取当前面多边形的弧度值
        function getDegree(ac, type) {
            var k;
            if(type === 'polygon') {
                k = 1;
            } else {
                k = Math.ceil(ac / 4);
            }
            for(k; k < ac; k += 1) {
                var flag = [], current = 0;
                initArray(flag, 1, ac);
                for(var j = 0; j < ac; j += 1) {
                    flag[current] = 0;
                    current = (current + k) % ac;
                    if(sum(flag, ac) === 0) {
                        return k * Math.PI * 2 / ac;
                    }
                }
            }
            return -1;
        }

        //排序数组初始化
        function initArray(arr, value, ac) {
            for(var i = 0; i < ac; i += 1) {
                arr[i] = value;
            }
        }

        //计算数组前n项和
        function sum(arr, n) {
            var result = 0;
            for(var i = 0; i < n; i += 1) {
                result += arr[i];
            }
            return result;
        }

        //角度转弧度
        function degToRad (deg) {
            return deg * Math.PI / 180;
        }
        
        return PolygonAndPolyhorns;
    })();
} else {
    throw new Error('环境错误，请在浏览器中使用');
}